
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_TourManagement : admin_BaseUserControl
{
    TourDAO daoTour = new TourDAO();
    Tour tour= new Tour();
    int id = 0;
    #region properties
    public string ImageURL
    {
        get { return tour.ImageURL; }
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        ActiveAccording = "tour";
        try
        {
            LoadData();
            if (Request["mode"] == "1")
            {
                //mode: edit
                lnkAdd.Visible = false;
                lnkUpdate.Visible = true;
                lnkDelete.Visible = true;
             
                    int id = int.Parse(Request["id"]);
                    CurrentID = Request["id"];
                    tour = daoTour.GetTour(id);
                    txtName.Text = tour.Name;
                    txtDescription.Text = tour.Description;
                    txtDuring.Text = tour.During.ToString();
                    txtPrice.Text = tour.Price.ToString();
                    txtDiscount.Text = tour.Discount.ToString();
                    txtTag.Text = tour.Tag;
                    fckContent.Value = tour.Article;
                    chkState.Checked = tour.State;
                    chkIsRecommended.Checked = tour.IsRecommended;
                    int index = 0;
                    foreach (ListItem item in ddlLocation.Items)
                    {
                        if (item.Value == tour.Location.Id.ToString())
                        {
                            ddlLocation.SelectedIndex = index;
                            break;
                        }
                        index++;
                    }
                    index = 0;
                    foreach (ListItem item in ddlRegion.Items)
                    {
                        if (item.Value == tour.Region.ToLower())
                        {
                            ddlRegion.SelectedIndex = index;
                            break;
                        }
                        index++;
                    }
                
            }
            else
            {
                //mode: add new
                txtName.Text = "";
                txtDescription.Text = "";
                fckContent.Value = "";
                txtTag.Text = "";
                txtPrice.Text = "0";
                txtDuring.Text = "0";
                txtDiscount.Text = "0";
                lnkAdd.Visible = true;
                lnkUpdate.Visible = false;
                lnkDelete.Visible = false;
            }
        }
        catch (Exception ex)
        {
            Response.Redirect("Default.aspx?ctrl=tour_list");
        }
       
    }
    public void LoadData()
    {
        LocationDAO daoLocation = new LocationDAO();
        ddlRegion.Items.Add(new ListItem("Northern","northern"));
        ddlRegion.Items.Add(new ListItem("Central","central"));
        ddlRegion.Items.Add(new ListItem("Southern","southern"));
        ddlLocation.DataSource = daoLocation.GetAllLocations();
        ddlLocation.DataTextField = "Name";
        ddlLocation.DataValueField = "Id";
        ddlLocation.DataBind();
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Tour tour = new Tour();
        TourDAO daoTour = new TourDAO();
        //i.Name, Location, Region, During, Description, , ImageURL,
        //Tag, Price,Discount, ISRecommended, ModifiedDate, ModifiedBy, State
        this.Page.Validate();
        if (!this.Page.IsValid) return;
        tour.Name = txtName.Text;
        tour.Location.Id = int.Parse(ddlLocation.SelectedValue);
        tour.Region = ddlRegion.SelectedItem.Text;
        tour.During = Int16.Parse(txtDuring.Text);
        tour.Description = txtDescription.Text;
        tour.ImageURL = Request["txtImage"];        
        tour.Tag = txtTag.Text;
        tour.Article = fckContent.Value;
        tour.Price = decimal.Parse(txtPrice.Text);
        tour.Discount = decimal.Parse(txtDiscount.Text);
        tour.IsRecommended = chkIsRecommended.Checked;
        tour.ModifiedDate = DateTime.Today;
        tour.State = chkState.Checked;
        tour.ModifiedBy = 1;
        if (daoTour.Insert(tour) > 0)
        {
            ShowMessage("Successfull.Tour is added");
            CurrentID = tour.Id.ToString(); ;
            lnkAdd.Visible = false;
            lnkUpdate.Visible = true;
            lnkDelete.Visible = true;
        }
        else
            ShowMessage("Error occurred. adding failed.");

    }
    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        id = int.Parse(CurrentID);
        if (daoTour.Delete(id) > 0)
        {
            ShowMessage("Specified tour information is deleted successfully");
        }
        else ShowMessage("Error has occured when trying to delete specified tour information");
    }


    /// <summary>
    /// Clear text fields value
    /// </summary>
    public void ClearFields()
    {
        txtName.Text = "";
        txtDescription.Text = "";
        txtTag.Text = "";
        txtDiscount.Text = "";
        txtPrice.Text = "";
        txtDuring.Text = "";
        fckContent.Value = "";
        chkIsRecommended.Checked = false;
        chkState.Checked = false;
    }


    protected void lnkUpdate_Click(object sender, EventArgs e)
    {

       // Tour tour = new Tour();      
        //i.Name, Location, Region, During, Description, , ImageURL,
        //Tag, Price,Discount, ISRecommended, ModifiedDate, ModifiedBy, State
        this.Page.Validate();
        if (!this.Page.IsValid) return;
        tour.Id = int.Parse(CurrentID);
        tour.Name = txtName.Text;
        tour.Location.Id = int.Parse(ddlLocation.SelectedValue);
        tour.Region = ddlRegion.SelectedItem.Text;
        tour.During = Int16.Parse(txtDuring.Text);
        tour.Description = txtDescription.Text;
        tour.ImageURL = Request["txtImage"];        
        tour.Tag = txtTag.Text;
        tour.Article = fckContent.Value;
        tour.Price = decimal.Parse(txtPrice.Text);
        tour.Discount = decimal.Parse(txtDiscount.Text);
        tour.IsRecommended = chkIsRecommended.Checked;
        tour.ModifiedDate = DateTime.Today;
        tour.State = chkState.Checked;
        tour.ModifiedBy = 1;
        if (daoTour.Update(tour) > 0)
        {
            ShowMessage("Tour information is updated successfully");
        }
        else ShowMessage("Error has occured when trying to update tour information");
    }
}
   
    

